using System.ComponentModel;
using Bee.Base.Abstractions.Navigation;
using Bee.Base.Helpers;
using Bee.Base.Models.Navigation;
using Bee.Base.ViewModels;
using Ke.Bee.Localization.Localizer.Abstractions;

namespace Bee.Base.Impl.Navigation;

/// <summary>
/// 默认视图导航器
/// </summary>
public class DefaultViewNavigator : IViewNavigate
{
    /// <summary>
    /// 本地化资源对象
    /// </summary>
    private readonly ILocalizer _l;

    /// <summary>
    /// 属性变化通知事件
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// 当前页视图模型
    /// </summary>
    public PageViewModelBase CurrentPage { get; private set; }
    private readonly IReadOnlyDictionary<string, INavigationCommand> _navigationCommands;

    private readonly IEnumerable<INavigationCommand> _commands;

    public DefaultViewNavigator(ILocalizer localizer, IEnumerable<INavigationCommand> commands)
    {
        _l = localizer;
        _commands = commands;
    }
    
    public void SetCurrentPage(PageViewModelBase page)
    {
        CurrentPage = page;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentPage)));
    }

    public void NavigateTo(string key)
    {
        ArgumentExceptionHelper.ThrowIfNullOrEmpty(key);
        
        var command = _commands.FirstOrDefault(c => c.Key == key);
        
        ArgumentExceptionHelper.ThrowIfNavigationCommandNull(command);
        
        command?.Execute(new NavigationCommandContext((this)));
    }
    
}